<template>
	<view>

		<view class="imginfo">
            <view style="flex-grow: 6;padding-top: 20rpx;">
				<image class="imginfo-img" :src='imagep' mode="aspectFit"></image>
			</view>
            <view style="flex-grow: 1;font-weight: bolder;font-size: 40rpx;">{{name}}</view>
            <view style="flex-grow: 1;font-weight: bolder;font-size: 34rpx;">{{projectName}}</view>
			<view style="flex-grow: 1;">
				 <view class="enshrine" @click="enshrines(1)" v-if="type==1"> + 收藏</view>
				 <view class="enshrine" @click="enshrines(2)" v-if="type==2"> - 取消</view>
			</view>
		</view>

		<view class="telinfo">
			<view class="tinfo" >
				<view style="flex-grow: 1;">机构 : </view>
				<view style="flex-grow: 9;text-align: left;">{{organizationName}}</view>
			</view>
			<view class="tinfo">
				<view style="flex-grow: 1;">电话 : </view>
				<view style="flex-grow: 9;text-align: left;">{{tel}}</view>
			</view>
		</view>
		<view class="info">
			<view style="flex-grow: 1;"></view>
			<view @click="bytel" >  
				<image class="imgs" src="../../static/images/dianhua.png" ></image><text>打电话</text>
			</view>
			<view @click="bysend"> 
			   <image  class="imgs" src="../../static/images/duanxin.png" ></image><text>发信息</text> 
			 </view>
			<view style="flex-grow: 1;"></view>
		</view>

	</view>
</template>

<script>
	let that;
	export default {
		data() {
			return {
               tel:"16596365555",
			   name:"赵子龙",
			   projectName:"常山开发区",
			   organizationName:"大蜀集团",
			   imagep:'http://img2.imgtn.bdimg.com/it/u=2951568718,900410320&fm=26&gp=0.jpg',
			   id:"",
               type:"",
			   loginName:"test"  ,//登录名,英文
			   modalName: null,
			   conversations:[],//会话列表
			   syncConversation:[],//离线消息
			   pathHost:""
			}
		},
		onLoad(options){
			that=this;
			uni.showLoading({
				title:"数据加载..",
				mask:true	
			})
			const val = uni.getStorageSync("pathHost");
			that.pathHost=val;
			//console.log(JSON.stringify(options))
			var obj=JSON.parse(options.data);
			/* console.log("详情页")
			console.log(JSON.stringify(obj)) */
			this.tel=obj.value;
			this.name=obj.name;
			this.projectName=obj.ptName;
			this.organizationName=obj.institution;
			this.imagep=obj.imgPath;
			this.id=obj.id;
			this.type=obj.typeInfo;
			this.loginName=obj.loginName;

			let userinfo = uni.getStorageSync("userInfo");
			that.$jjim.config(userinfo.data.body.username,userinfo.data.body.pwd);
			//获取会话列表
			that.$jjim.getConversation().then(function(data){				
				that.conversations = data
				console.log(that.conversations.length)
				for(var i = 0; i < data.length; i++){
					let _c = data[i]
					that.$jjim.getResurce(data[i].avatar).then(function(data){
						_c.avatar = data
						that.conversations[i] = _c
					},function(){})
				}
				that.mergeConversations();
				
			//	根据会话列表,查看是否与点击用户有会话信息,如果有读出历史消息
				that.getConversation();
				 uni.hideLoading();
			},function(error){
				console.log(error)
			})
			
			//离线
			that.$jjim.onSyncConversation(function(_d){
				that.syncConversation = _d
				console.log("离线消息监听")
				console.log(that.syncConversation)
				that.mergeConversations()
			})
			
		},
		onShow(){
			
		},
		methods: {
			mergeConversations(){
				if(this.conversations.length > 0 && this.syncConversation.length > 0){
					let conversations = this.conversations
					for(var i = 0; i< conversations.length;i++){
						let _c = conversations[i]
						for(var j = 0;j < this.syncConversation.length;j++){
							var from_username = this.syncConversation[j].from_username
							if(_c.username == from_username){
								var msgs = this.syncConversation[j].msgs
								var msg_text = "暂无消息"
								
								if(msgs && msgs.length > 0){
									let pop = msgs.pop();
									if(pop.content.msg_type == "image")
										msg_text = "[图片]";
									else
										msg_text = pop.content.msg_body.text
								}
								_c.msg_text = msg_text
								// _c.msgs = msgs
								break;
							}
						}
						this.conversations[i] = _c
					}
				}
			},
             enshrines:function(type){
			      //保存收藏
				//  console.log(this.tel+":"+this.name)
				
				  try{
					  const value = uni.getStorageSync("JSESSIONID")
					  if (value) {
						  uni.showLoading({
						  	title:"保存中..",
						  	mask:true	
						  })
						  if(type==1){
							  uni.request({
							      data:{
							  	  uid:""+that.id,
							  	  uname:""+that.name,
							  	  utel:""+that.tel,
							  	  uimg:""+that.imagep,
							  	  uprojcet:""+that.projectName,
							  	  uinstitution:""+that.organizationName								 
							  	  },
							  	  url: that.pathHost+'/ggcapp/userprojects/userenshrine/userEnshrine/save'+";JSESSIONID="+value+"?__ajax=true&mobileLogin=true" ,
							  	  success(obj) {
							  	      // console.log(obj);
							  	       if(obj.data.success){
							  				 uni.showToast({
							  					 title: ''+obj.data.msg,
							  					 duration: 1600
							  				 });
											 setTimeout(function(){
												  uni.hideLoading();
												  uni.navigateBack({
												      delta: 1
												  });
											 },1600)
							  			 }
							  		  }
							  		  
							    });
						  }else if(type==2){
							  uni.showLoading({
							  	title:"取消中..",
							  	mask:true	
							  })
							  uni.request({
							      data:{
							  	  ids:""+that.id 							 
							  	  },
							  	  url: that.pathHost+'/ggcapp/userprojects/userenshrine/userEnshrine/delete'+";JSESSIONID="+value+"?__ajax=true&mobileLogin=true" ,
							  	  success(obj) {
							  	      // console.log(obj);
							  	       if(obj.data.success){
							  				 uni.showToast({
							  					 title: ''+obj.data.msg,
							  					 duration: 1600
							  				 });
											 setTimeout(function(){
											 	 uni.hideLoading();
												 uni.navigateBack({
												     delta: 1
												 });
											 },1600)
							  			 }
							  		  }
							  		  
							    });
						  }
  
						}	
				  }catch (e) {
					   uni.showToast({
						title: 'erro',
						icon: 'none'
					   })
			      }
  
			 },
			 bytel:function(){
				 console.log(this.tel)			
				  uni.showActionSheet({
				 	itemList: ['电话联系'],
				 	success: (e) => {
				 		if (e.tapIndex == 0) {
				 			uni.makePhoneCall({
				 				phoneNumber: this.tel
				 			});
				 		}
				 	}
				 }) 
				 
				 
			 },
			 bysend:function(){
				  
				  
				  uni.navigateTo({
				  	url: '../addressList/char'
				  })
  
			 },
			 //获取会话列表
			 getConversation:function(){
				/* console.log("获取会话列表")
				  console.log(this.conversations.length)
				 console.log(this.name)
				 console.log(this.loginName) */
				 let boo=false;
				 for (var i = 0; i < this.conversations.length; i++) {
				 	 if(this.conversations[i].type==3){ //单聊的 ，非群聊 4
				 		 if(this.conversations[i].name==this.loginName){
				 			 let chater_info=this.conversations[i];
				 			 uni.setStorageSync('jiguang_chater_info',chater_info)
				 		 // console.log(chater_info);
						   boo=true;
						   break;
				 		 }
				 	 }
				 }
				  console.log(boo)
				 if(!boo){
				    uni.setStorageSync('jiguang_chater_info',{})
				    uni.showToast({
				    	title: '没有历史会话消息',
				    	icon: 'none'
				    }) 
				 }
				 
				 
			 }
		}
	}
</script>

<style>
	.imginfo {
		width: 100%;
		height: 520rpx;
		background-color: #DCDFE6;
		background-image: url('http://img1.imgtn.bdimg.com/it/u=2660858002,944531253&fm=26&gp=0.jpg')  ;
        display: flex;
		flex-grow: 10;
		flex-direction:column;
		text-align: center;
		
		/* opacity:0.3 */
	}
    .imginfo-img{
		width: 260rpx;
		height: 260rpx;
		border-radius: 50%;
		background-color: #C0C0C0;
	}
	.telinfo {
		margin-top: 60rpx;
		margin-left: 10rpx;
		margin-right: 10rpx;
		width: 90%%;
		height: 180rpx;
		background-color: white;
		text-align: center;	
	}
	.tinfo{
		display: flex;
		flex-grow: 10;
		width: 100%;
		height: 50%;
		line-height: 90rpx;
	}

	.info {
		display: flex;
         margin-top: 40rpx;
	}
	.imgs{
		    position: relative;
		    top: 16rpx;
		    left: -12rpx;
			width: 60rpx;
			height: 60rpx;
	}
	
	.info>view:nth-of-type(2){
		flex-grow: 1;
		height: 80rpx;
		line-height: 80rpx;
		text-align: center;
		
	}
	.info>view:nth-of-type(3){
		flex-grow: 1;
		height: 80rpx;
		line-height: 80rpx;
		text-align: center;
	}
	.enshrine{
		width: 160rpx;
		height: 50rpx;
		border: 1px solid white;
		color: white;
		float: right;
		margin-right: 60rpx;
		border-radius: 5rpx;
		
	}
	
	
	
	/* //点击时 */
	.clickhove:hover{
		background-color: #C0C0C0;
	}
	
</style>
